package com.jiyinit.web.core.business.service;

import com.jiyinit.web.core.business.dao.TabCoreMemoireDao;
import com.jiyinit.web.core.business.dto.MemoireRequest;
import com.jiyinit.web.core.business.entity.TabCoreMemoireEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by lr on 2018/1/4.
 */
@Service("core.memoireService")
public class MemoireServiceImpl {

    @Resource(name = "core.tabCoreMemoireDao")
    private TabCoreMemoireDao tabCoreMemoireDao;

    public List<TabCoreMemoireEntity> findRemind(){
        return tabCoreMemoireDao.findRemind();
    }

    public int updateList(List<TabCoreMemoireEntity> list){
        return tabCoreMemoireDao.update(list);
    }

    public int add(MemoireRequest request){
        int num =0;
        TabCoreMemoireEntity entity = new TabCoreMemoireEntity();
        entity.setContent(request.getContent());
        entity.setStartTime(request.getStartTime());
        entity.setEndTime(request.getEndTime());
        entity.setRemark(request.getRemark());
        entity.setRemindType(request.getRemindType());
        //计算
        if(request.getStartTime() != null && request.getRemindType() != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(request.getStartTime());
            if (request.getRemindType().intValue() == CommonEnum.RemindType.START.getCode().intValue()) {
                //2:开始时
                entity.setRemindTime(request.getStartTime());
            } else if (request.getRemindType().intValue() == CommonEnum.RemindType.MIN5.getCode().intValue()) {
                //3：提前5分钟
                calendar.add(Calendar.MINUTE, -5);
                entity.setRemindTime(calendar.getTime());
            } else if (request.getRemindType().intValue() == CommonEnum.RemindType.MIN15.getCode().intValue()) {
                //4：提前15分钟
                calendar.add(Calendar.MINUTE, -15);
                entity.setRemindTime(calendar.getTime());
            } else if (request.getRemindType().intValue() == CommonEnum.RemindType.MIN30.getCode().intValue()) {
                //5：提前30分钟
                calendar.add(Calendar.MINUTE, -30);
                entity.setRemindTime(calendar.getTime());
            } else if (request.getRemindType().intValue() == CommonEnum.RemindType.HOUR.getCode().intValue()) {
                //6：提前一小时
                calendar.add(Calendar.HOUR, -1);
                entity.setRemindTime(calendar.getTime());
            } else if (request.getRemindType().intValue() == CommonEnum.RemindType.CUSTOM.getCode().intValue()) {
                //67：自定义时间
                entity.setRemindTime(request.getRemindTime());
            }
        }
        entity.setRepeatType(request.getRepeatType());
        entity.setCreateId(request.getUserId());
        entity.setCreateTime(new Date());
        entity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
        num = tabCoreMemoireDao.add(entity);
        return num;
    }

    public int delete(Integer id,Integer userId){
        return tabCoreMemoireDao.delete(id,userId);
    }

    public static void main(String[] args){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MINUTE,-5);
       System.out.print(calendar.getTime());
    }
}
